package com.springboot.study.tests.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: guodong
 * @Date: 2021/7/24 12:51
 * @Version: 1.0
 * @Description:
 */
public class EnumStringValidator implements ConstraintValidator<EnumValidateAnnotation,String> {

    private List<String> enumStrings;

    @Override
    public void initialize(EnumValidateAnnotation constraintAnnotation) {
        enumStrings = Arrays.asList(constraintAnnotation.enums());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }
        // 对于Integer，将其转化为String 后比较
        return enumStrings.contains(value);
    }
}
